package com.bianguo.android.iocdemo.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;

import com.bianguo.android.injectlibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;

public class PayPwdView extends AppCompatEditText {

    private static final int MAX_LENGTH = 6;
    private int mWidth, mHeight;
    private RectF mRectF, focusedRectF;
    private Paint mPaint, circlePaint, focusedPaint;
    private int divideLineStartX;
    private int position;
    private OnPwdChangeListener onPwdChangeListener;

    public PayPwdView(Context context) {
        this(context, null);
    }

    public PayPwdView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PayPwdView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)});
        setCursorVisible(false);
        setBackgroundColor(Color.TRANSPARENT);
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    }

    private void init(Context context, AttributeSet attrs) {
        mRectF = new RectF();
        focusedRectF = new RectF();

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GRAY);

        circlePaint = new Paint();
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setStrokeWidth(6);
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.BLACK);

        focusedPaint = new Paint();
        focusedPaint.setStyle(Paint.Style.STROKE);
        focusedPaint.setStrokeWidth(8);
        focusedPaint.setAntiAlias(true);
        focusedPaint.setColor(ActivityCompat.getColor(context, R.color.blue));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

        divideLineStartX = w / MAX_LENGTH;
        mRectF.set(0, 0, mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.drawRoundRect(mRectF, 20, 20, mPaint);
        for (int i = 0; i < MAX_LENGTH - 1; i++) {
            canvas.drawLine((i + 1) * divideLineStartX, 0, (i + 1) * divideLineStartX, mHeight, mPaint);
        }
        drawFocusedItem(canvas, position);
        drawPwdCircle(canvas, position);
    }

    private void drawFocusedItem(Canvas canvas, int position) {
        if (position >= MAX_LENGTH) {
            return;
        }
        focusedRectF.set(position * divideLineStartX, 0, (position + 1) * divideLineStartX, mHeight);
        canvas.drawRoundRect(focusedRectF, 0, 0, focusedPaint);
    }

    private void drawPwdCircle(Canvas canvas, int position) {
        if (position > MAX_LENGTH) {
            return;
        }
        for (int i = -1; i < position; i++) {
            canvas.drawCircle(i * divideLineStartX + mWidth / MAX_LENGTH / 2, mHeight / 2, 6, circlePaint);
        }
    }


    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        MLog.i("text = " + text + ", start = " + start + ", lengthBefore = " + lengthBefore + ", lengthAfter = " + lengthAfter);
        this.position = start + lengthAfter;
        if (this.position <= MAX_LENGTH) {
            invalidate();
        }
        if (position == MAX_LENGTH) {
            if (onPwdChangeListener != null) {
                onPwdChangeListener.inputFinished(getPasswordStr());
            }
        }
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if (selStart == selEnd) {
            setSelection(getText().length());
        }
    }

    public String getPasswordStr() {
        return getText().toString().trim();
    }

    public void setOnPwdChangeListener(OnPwdChangeListener onPwdChangeListener) {
        this.onPwdChangeListener = onPwdChangeListener;
    }

    public interface OnPwdChangeListener {

        void inputFinished(String pwd);
    }
}
